package com.jc.auth.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.jc.auth.common.enums.IsDeletedFlagEnum;
import com.jc.auth.domain.constants.AuthConstant;
import com.jc.auth.domain.convert.AuthPermissionBOConverter;
import com.jc.auth.domain.convert.AuthRoleBOConverter;
import com.jc.auth.domain.entity.AuthPermissionBO;
import com.jc.auth.domain.entity.AuthRoleBO;
import com.jc.auth.domain.redis.RedisUtil;
import com.jc.auth.domain.service.AuthPermissionDomainService;
import com.jc.auth.infra.basic.entity.AuthPermission;
import com.jc.auth.infra.basic.entity.AuthRole;
import com.jc.auth.infra.basic.mapper.AuthPermissionDao;
import com.jc.auth.infra.basic.service.AuthPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: ASL_ly
 * @Package: com.jc.auth.domain.service.impl
 * @Project: jc-club
 * @Date: 2024/10/29  13:04
 */
@Service
@Slf4j
public class AuthPermissionDomainServiceImpl implements AuthPermissionDomainService {
    @Resource
    private AuthPermissionService authPermissionService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 新增权限
     * @param authPermissionBO
     * @return
     */
    @Override
    public Boolean add(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("permissionController.add.bo:{}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionBOConverter
                .INSTANCE.convertBOToPermission(authPermissionBO);
        authPermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.code);
        Integer count = authPermissionService.insert(authPermission);
        return count > 0;
    }

    /**
     * 修改角色
     * @param authPermissionBO
     * @return
     */
    @Override
    public Boolean update(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("permissionController.update.bo:{}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionBOConverter
                .INSTANCE.convertBOToPermission(authPermissionBO);
        Integer count = authPermissionService.update(authPermission);
        return count > 0;
    }

    /**
     * 删除角色
     * @param authPermissionBO
     * @return
     */
    @Override
    public Boolean delete(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("permissionController.delete.bo:{}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionBOConverter
                .INSTANCE.convertBOToPermission(authPermissionBO);
        authPermission.setIsDeleted(IsDeletedFlagEnum.DELETED.code);
        Integer count = authPermissionService.update(authPermission);
        return count > 0;
    }

    /**
     * 查询用户权限
     * @param userName
     * @return
     */
    @Override
    public List<String> getPermission(String userName) {
        String permissionKey = redisUtil.buildKey(AuthConstant.authPermissionPrefix, userName);
        String permissionValue = redisUtil.get(permissionKey);
        if(StringUtils.isBlank(permissionValue)){
            return Collections.emptyList();
        }
        List<AuthPermission> permissionList = new Gson().fromJson(permissionValue,
                new TypeToken<List<AuthPermission>>(){
                }.getType());
        List<String> authList = permissionList.stream()
                .map(AuthPermission::getPermissionKey).collect(Collectors.toList());
        return authList;
    }
}
